package com.lagou.stage04;

/**
 * 题目：编写程序，循环接收用户从键盘输入多个字符串，直到输入“end”时循环结束，并将所有已输入的字符串按字典顺序倒序录入到项目下abc.txt中
 * */

import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class InPutAndOutPutTest {
    public static void main(String[] args) {
        BufferedReader br = null;
        PrintWriter pw = null;
        List<String> list = new ArrayList<String>();
        br = new BufferedReader(new InputStreamReader(System.in));
        try {
            String str = "";
            System.out.println("Please input some strings:");
            while (!"end".equals(str)) {
                str = br.readLine();
                list.add(str);
            }
            Collections.sort(list, Collections.reverseOrder());
            pw = new PrintWriter("./abc.txt");
            for (String s: list) {
                System.out.println(s);
                pw.println(s);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != pw) {
                pw.close();
            }
            if (null != br) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
